﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BTv2.Controls;
using BTv2.Model;

namespace BTv2.UserControls
{
    /// <summary>
    /// Interaction logic for SensorSettings.xaml
    /// </summary>
    public partial class SensorSettings : UserControl
    {

        private Settings _settingsWindow;
        private SensorsController _scontroller;
        public SensorSettings()
        {
            InitializeComponent();
        }
        public SensorSettings(Settings s)
        {
            InitializeComponent();
            _settingsWindow = s;
        }

        private void SensorListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var sensor = SensorListBox.SelectedItem as Sensor;
            try
            {
                SensorListContent.IsEnabled = false;
                SensorSelectedContent.DataContext = sensor;
                SensorNameTextbox.IsEnabled = false;
                SensorSelectedContent.IsEnabled = true;
                SensorMunitCombobox.ItemsSource = _scontroller.SensorMetricUnits;
                SensorTypeCombobox.ItemsSource = _scontroller.SensorTypes;
            }
            catch
            {
                //when deselecting its called
            }
        }
        private void SensorSettingsAddType(object sender, RoutedEventArgs e)
        {
            var stw= new SensorTypeWindow(this);
            stw.Show();
            SSettingsCreateType.Click -= SensorSettingsAddType;
        }

        private void SensorSettingsAddMUnit(object sender, RoutedEventArgs e)
        {
            var smuw = new SensorMUnitWindow(this);
            smuw.Show();
            SSettingsCreateMUnits.Click -= SensorSettingsAddMUnit;
        }
        public void AttachMUnitCreate()
        {
            SSettingsCreateMUnits.Click += SensorSettingsAddMUnit;
        }


        public void AttachTypeCreate()
        {
            SSettingsCreateType.Click += SensorSettingsAddType;
        }

        private void SensorSettingsRightClick(object sender, MouseButtonEventArgs e)
        {
            SensorSettingsMenu.IsOpen = !SensorSettingsMenu.IsOpen;
        }

        private void SensorSettingsBackButton(object sender, RoutedEventArgs e)
        {
            _settingsWindow.OpenMainSettings();
        }

        private void SensorsTabLoaded(object sender, RoutedEventArgs e)
        {
            SensorSelectedContent.IsEnabled = false;
            _scontroller = SensorsController.GetInstance();
            SensorListBox.ItemsSource = _scontroller.Sensors;
            IndustrialPCIDSComboBox.ItemsSource = IndustrialPCController.IndustrialPCIDS;
            SensorMunitCombobox.ItemsSource = _scontroller.SensorMetricUnits;
            SensorTypeCombobox.ItemsSource = _scontroller.SensorTypes;
        }
        public async void ASS_Button_CLicked(object sender, RoutedEventArgs e)
        {
            SensorSettingsMenu.IsOpen = false;
            SensorListContent.IsEnabled = false;
            SensorSelectedContent.DataContext = new Sensor();
            SensorNameTextbox.IsEnabled = true;
            SensorSelectedContent.IsEnabled = true;
        }


        private void CSS_Button_Clicked(object sender, RoutedEventArgs e)
        {
            SensorListBox.UnselectAll();
            SensorSelectedContent.DataContext = null;
            SensorSelectedContent.IsEnabled = false;
            SensorListContent.IsEnabled = true;
            SensorEnabledToggleSwitch.IsChecked = false;
        }

        private async void DSS_Button_Clicked(object sender, RoutedEventArgs e)
        {

            Sensor _s = SensorSelectedContent.DataContext as Sensor;
            SensorListBox.UnselectAll();
            SensorSelectedContent.DataContext = null;
            SensorSelectedContent.IsEnabled = false;
            SensorListContent.IsEnabled = true;
            SensorEnabledToggleSwitch.IsChecked = false;

            bool result = await Task.Run(() => { return _scontroller.DeleteSensor(_s); });
            if (result) _scontroller.Sensors.Remove(_s);
           Task.Run(async() => {
                NotificationsController.GetInstance().AddNotification(""+_s.Name+" was Deleted", NotificationsController.CryticalNotificationType);
                await UsersController.GetInstance().SendWarningSensorMail("", _s.Name, UsersController.GetInstance().GetLoggedUser().UserName);
            });
        }

        public void RefreshComboBoxes()
        {
            SensorMunitCombobox.ItemsSource = null;
            SensorTypeCombobox.ItemsSource = null;
            SensorMunitCombobox.ItemsSource = _scontroller.SensorMetricUnits;
            SensorTypeCombobox.ItemsSource = _scontroller.SensorTypes;
        }

        private async void SUA_Button_Clicked(object sender, RoutedEventArgs e)
        {
            Sensor _s = SensorSelectedContent.DataContext as Sensor;
            if (_s.ID == 0)
            {
                double res = 0;
                if (SensorNameTextbox.Text == "" || SensorTypeCombobox.SelectedValue == null || SensorMunitCombobox.SelectedValue == null || IndustrialPCIDSComboBox.SelectedItem == null || !double.TryParse(SensorMinValueTextbox.Text, out res) || !double.TryParse(SensorMaxValueTextbox.Text, out res) || !double.TryParse(SensorXValue.Text, out res) || !double.TryParse(SensorYValue.Text, out res) || !double.TryParse(SensorZValue.Text, out res))
                {
                        if(SensorNameTextbox.Text == "" ) SensorNameTextbox.Background = new SolidColorBrush(Colors.Red);
                        if(SensorTypeCombobox.SelectedValue == null) SensorTypeCombobox.Background = new SolidColorBrush(Colors.Red);
                        if(SensorMunitCombobox.SelectedValue == null) SensorMunitCombobox.Background = new SolidColorBrush(Colors.Red);
                    if(IndustrialPCIDSComboBox.SelectedItem == null ) IndustrialPCIDSComboBox.Background = new SolidColorBrush(Colors.Red);
                    if(!double.TryParse(SensorMinValueTextbox.Text, out res) ) SensorMinValueTextbox.Background = new SolidColorBrush(Colors.Red);
                    if(!double.TryParse(SensorMaxValueTextbox.Text, out res) ) SensorMaxValueTextbox.Background = new SolidColorBrush(Colors.Red);
                    if(!double.TryParse(SensorXValue.Text, out res) ) SensorXValue.Background = new SolidColorBrush(Colors.Red);
                    if(!double.TryParse(SensorYValue.Text, out res) ) SensorYValue.Background = new SolidColorBrush(Colors.Red);
                    if(!double.TryParse(SensorZValue.Text, out res) ) SensorZValue.Background = new SolidColorBrush(Colors.Red);
                    await Task.Delay(4000);
                         SensorNameTextbox.Background = new SolidColorBrush(Colors.White);
                         SensorTypeCombobox.Background = new SolidColorBrush(Colors.White);
                         SensorMunitCombobox.Background = new SolidColorBrush(Colors.White);
                         IndustrialPCIDSComboBox.Background = new SolidColorBrush(Colors.White);
                         SensorMinValueTextbox.Background = new SolidColorBrush(Colors.White);
                         SensorMaxValueTextbox.Background = new SolidColorBrush(Colors.White);
                         SensorXValue.Background = new SolidColorBrush(Colors.White);
                         SensorYValue.Background = new SolidColorBrush(Colors.White);
                         SensorZValue.Background = new SolidColorBrush(Colors.White);

                }else{

                _s.Name = SensorNameTextbox.Text;
                _s.Type = SensorTypeCombobox.SelectedValue as string;
                _s.MUnit = SensorMunitCombobox.SelectedValue as string;
                _s.IndustrialPCID = IndustrialPCIDSComboBox.SelectedItem as string;
                _s.Status =(bool) SensorEnabledToggleSwitch.IsChecked;
                _s.MinValue = double.Parse(SensorMinValueTextbox.Text);
                _s.MaxValue = double.Parse(SensorMaxValueTextbox.Text);
                _s.X = double.Parse(SensorXValue.Text);
                _s.Y = double.Parse(SensorYValue.Text);
                _s.Z = double.Parse(SensorZValue.Text);
                bool result = await Task.Run(() => { return _scontroller.AddSensor(_s); });
                if (result) _scontroller.Sensors.Add(_s);

                SensorListBox.UnselectAll();
                SensorSelectedContent.DataContext = null;
                SensorSelectedContent.IsEnabled = false;
                SensorListContent.IsEnabled = true;
                SensorEnabledToggleSwitch.IsChecked = false;

                }
            }
            else
            {
                double res = 0;
                if (SensorNameTextbox.Text == "" || SensorTypeCombobox.SelectedValue == null || SensorMunitCombobox.SelectedValue == null || IndustrialPCIDSComboBox.SelectedItem == null || !double.TryParse(SensorMinValueTextbox.Text, out res) || !double.TryParse(SensorMaxValueTextbox.Text, out res) || !double.TryParse(SensorXValue.Text, out res) || !double.TryParse(SensorYValue.Text, out res) || !double.TryParse(SensorZValue.Text, out res))
                {
                    if (SensorNameTextbox.Text == "") SensorNameTextbox.Background = new SolidColorBrush(Colors.Red);
                    if (SensorTypeCombobox.SelectedValue == null) SensorTypeCombobox.Background = new SolidColorBrush(Colors.Red);
                    if (SensorMunitCombobox.SelectedValue == null) SensorMunitCombobox.Background = new SolidColorBrush(Colors.Red);
                    if (IndustrialPCIDSComboBox.SelectedItem == null) IndustrialPCIDSComboBox.Background = new SolidColorBrush(Colors.Red);
                    if (!double.TryParse(SensorMinValueTextbox.Text, out res)) SensorMinValueTextbox.Background = new SolidColorBrush(Colors.Red);
                    if (!double.TryParse(SensorMaxValueTextbox.Text, out res)) SensorMaxValueTextbox.Background = new SolidColorBrush(Colors.Red);
                    if (!double.TryParse(SensorXValue.Text, out res)) SensorXValue.Background = new SolidColorBrush(Colors.Red);
                    if (!double.TryParse(SensorYValue.Text, out res)) SensorYValue.Background = new SolidColorBrush(Colors.Red);
                    if (!double.TryParse(SensorZValue.Text, out res)) SensorZValue.Background = new SolidColorBrush(Colors.Red);
                    await Task.Delay(4000);
                    SensorNameTextbox.Background = new SolidColorBrush(Colors.White);
                    SensorTypeCombobox.Background = new SolidColorBrush(Colors.White);
                    SensorMunitCombobox.Background = new SolidColorBrush(Colors.White);
                    IndustrialPCIDSComboBox.Background = new SolidColorBrush(Colors.White);
                    SensorMinValueTextbox.Background = new SolidColorBrush(Colors.White);
                    SensorMaxValueTextbox.Background = new SolidColorBrush(Colors.White);
                    SensorXValue.Background = new SolidColorBrush(Colors.White);
                    SensorYValue.Background = new SolidColorBrush(Colors.White);
                    SensorZValue.Background = new SolidColorBrush(Colors.White);

                }
                else
                {

                    _s.Name = SensorNameTextbox.Text;
                    _s.Type = SensorTypeCombobox.SelectedValue as string;
                    _s.MUnit = SensorMunitCombobox.SelectedValue as string;
                    _s.IndustrialPCID = IndustrialPCIDSComboBox.SelectedItem as string;
                    _s.Status = (bool)SensorEnabledToggleSwitch.IsChecked;
                    _s.MinValue = double.Parse(SensorMinValueTextbox.Text);
                    _s.MaxValue = double.Parse(SensorMaxValueTextbox.Text);
                    _s.X = double.Parse(SensorXValue.Text);
                    _s.Y = double.Parse(SensorYValue.Text);
                    _s.Z = double.Parse(SensorZValue.Text);
                    bool result = await Task.Run(() => { return _scontroller.SaveSensor(_s); });

                    SensorListBox.UnselectAll();
                    SensorSelectedContent.DataContext = null;
                    SensorSelectedContent.IsEnabled = false;
                    SensorListContent.IsEnabled = true;
                    SensorEnabledToggleSwitch.IsChecked = false;
                }
            }

        }
    }
}
